package com.cwf.metadatamanage.controller;

import com.cwf.metadatamanage.consts.Constants;
import com.cwf.metadatamanage.model.dtos.TableInfo;
import com.cwf.metadatamanage.model.vo.ResultResponse;
import com.cwf.metadatamanage.model.vo.req.ColumnReq;
import com.cwf.metadatamanage.model.vo.req.TableReq;
import com.cwf.metadatamanage.service.MetadataShowService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;

@Api(value = "/元数据展示", tags = "元数据展示", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
@ApiResponses(@ApiResponse(code = 404, message = "metadata show not found"))
@RestController
@RequestMapping(value = Constants.BASE_API_PATH + "/metadataShow", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class MetadataShowController extends BaseController {

    @Autowired
    private MetadataShowService metadataShowService;

    /**
     * 获取某个系统下的所有database下的所有table
     *
     * @param tableReq
     * @param bindingResult
     * @param request
     * @return
     */
    @ApiOperation(value = "获取某个系统下的所有database下的所有table")
    @GetMapping("/getTables")
    public ResponseEntity<ResultResponse<List<TableInfo>>> getTables(
            @Valid @RequestBody TableReq tableReq,
            BindingResult bindingResult,
            HttpServletRequest request) {
        List<TableInfo> tableInfoList = this.metadataShowService.getTables(tableReq);
        return ResponseEntity.ok(new ResultResponse().success(tableInfoList));
    }


    /**
     * 获取某个table下的所有元数据
     *
     * @param columnReq
     * @param bindingResult
     * @param request
     * @return
     */
    @ApiOperation(value = "获取某个table下的所有元数据")
    @GetMapping("/getTableInfoAndColumns")
    public ResponseEntity<ResultResponse<TableInfo>> getTableInfoAndColumns(
            @Valid @RequestBody ColumnReq columnReq,
            BindingResult bindingResult,
            HttpServletRequest request) {

        TableInfo tableInfo = this.metadataShowService.getTableInfoAndColumns(columnReq);
        return ResponseEntity.ok(new ResultResponse().success(tableInfo));
    }


}
